package edu.msu.cse.meldru20.project1;


import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import edu.msu.cse.meldru20.project1.EditingView.Drawing;

public class GuessActivity extends Activity {
	
	private GameManager game = null;	
	private TextView player1score = null; 
	private TextView player2score = null;
	private TextView category = null;
	private TextView hint = null; 
	private TextView timerText = null;
	private EditText guessedText;
	private int TimeLeft = 0;
	private int second = 1;
	
	private static final String TIMELEFT = "TimeLeft";
	
	private MyCounter timer; 
	
	private boolean drawing = true;

	private static final String DRAWING = "drawing";

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guess);
		game = GameManager.get();
		
		player1score = (TextView)findViewById(R.id.Player1Score);
		player2score = (TextView)findViewById(R.id.Player2Score);
		category = (TextView)findViewById(R.id.Category);
		hint = (TextView)findViewById(R.id.Hint);
		guessedText = (EditText)findViewById(R.id.GuessText);
		timerText = (TextView)findViewById(R.id.Timer);

		
		
		
		timer = new MyCounter(game.getTimer(), second);
		timer.start();
		
		
		
		
		// Get the guess image view
		EditingView guess = (EditingView)findViewById(R.id.editingView1);
		
		// Make it no longer editable
		guess.SetEditable(false);
		
		// Set the drawing
		Intent intent = getIntent();
		Drawing theDrawing = (Drawing)intent.getSerializableExtra(EditingActivity.TRANSFER);
		guess.SetDrawing(theDrawing);
		
		if(savedInstanceState != null) {
			TimeLeft = game.getTimer();
			}

		
		setTitle("Round " + game.getCurrentRound() + ": " + game.getCurrentPlayer() + " is Guessing");

		updateUI();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle bundle) {
		super.onSaveInstanceState(bundle);
		bundle.putInt(TIMELEFT, TimeLeft);
		Log.i("time", ""+TimeLeft);
		bundle.putBoolean(DRAWING, drawing);
	}
	
	public void onSubmit(View view) {
		if (guessedText.getText().toString().toLowerCase(Locale.getDefault()).equals(game.getDrawingName().toLowerCase(Locale.getDefault()))){
			addScore();
			correct();
		}
		else{
			guessedText.setText("");
		}
	}
	
	public class MyCounter extends CountDownTimer{		
		public MyCounter(long millisInFuture, long countDownInterval){
			super(millisInFuture, countDownInterval);
		}
		
		@Override
		public void onTick(long millisUntilFinished){
			TimeLeft = (int) millisUntilFinished / 1000;
			timerText.setText("Time Remaining: " + TimeLeft + " seconds");
			if (TimeLeft <= 60){
				hint.setText(game.getDrawingHint());
			}
			else{
				hint.setText("");
			}
			TimeLeft = TimeLeft*1000;
			game.setTimer(TimeLeft);
		}

		@Override
		public void onFinish() {	
			timeExpired();
		}
	}

	private void timeExpired(){
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setTitle("Out of Time!");
		alertDialogBuilder.setMessage("You have run out of time!  The correct answer was " + game.getDrawingName());
		alertDialogBuilder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				game.advanceGame(getBaseContext());
			}
		});
		AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.show();
	}
	
	public void correct(){
		timer.cancel();
		TimeLeft = 130000;
		game.setTimer(130000);
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setTitle("Correct!");
		alertDialogBuilder.setMessage("That is Correct!");
		alertDialogBuilder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				game.advanceGame(getBaseContext());
			}
		});
		AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.show();
	}
	
	
    /**
     * Ensure the user interface components match the current state
     */
    private void updateUI() {
		category.setText("Category: " + game.getCurrentCategory());
    	player1score.setText(game.getPlayer1() + ": " + game.getPlayer1Score());
    	player2score.setText(game.getPlayer2() + ": " + game.getPlayer2Score());
    }
	
    
    private void addScore(){
    	String currentPlayer = game.getCurrentPlayer();
    	if (currentPlayer == game.getPlayer1()){
    		int currentPlayerScore = game.getPlayer1Score();
    		currentPlayerScore += TimeLeft;
    		game.setPlayer1Score(currentPlayerScore);
    	}
    	else{
    		int currentPlayerScore = game.getPlayer2Score();
    		currentPlayerScore += TimeLeft;
    		game.setPlayer2Score(currentPlayerScore);
    	}
    }
    
    
    
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 * Capture a "back" button press and force the user to quit back to the
	 * main menu or keep playing
	 */
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
        	game.handleBackButton(this);
        	return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

